package com.lxy.dao;

import com.lxy.entity.RegisterOrder;
import com.lxy.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RegisterationDao {

    public List<RegisterOrder> query() {
        List<RegisterOrder> registerOrderList = new ArrayList<>();
        Connection connection = null;
        try {
            connection = DBUtils.getConnection();
            String query = "select * from registerordery";
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            ResultSet set = preparedStatement.executeQuery();
            while (set.next()) {
                RegisterOrder registerOrder = new RegisterOrder();
                registerOrder.setRoid(set.getInt("roid"));
                registerOrder.setMemberid(set.getInt("memberid"));
                registerOrder.setDepartid(set.getInt("departid"));
                registerOrder.setDocid(set.getInt("docid"));
                registerOrder.setRotime(set.getString("rotime"));
                registerOrder.setStatus(set.getInt("status"));
                registerOrder.setOptime(set.getString("optime"));
                registerOrderList.add(registerOrder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return registerOrderList;
    }

    public RegisterOrder registerQuery(Integer roid) {
        RegisterOrder registerOrder = null;
        String query = "SELECT * FROM registerordery WHERE roid=?";
        try (Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(query)) {
            ps.setInt(1, roid);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    registerOrder = new RegisterOrder();
                    registerOrder.setRoid(rs.getInt("roid"));
                    registerOrder.setDepartid(rs.getInt("departid"));
                    registerOrder.setMemberid(rs.getInt("memberid"));
                    registerOrder.setDocid(rs.getInt("docid"));
                    registerOrder.setRotime(rs.getString("rotime"));
                    registerOrder.setStatus(rs.getInt("status"));
                    registerOrder.setOptime(rs.getString("optime"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return registerOrder;
    }

    public Integer updateRegisterorder(Integer roid, String property, String propertyValue) throws SQLException {
        Integer affectedRows = 0;
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = DBUtils.getConnection();
            String query = "UPDATE registerordery SET " + property + "=? WHERE roid=?";
            ps = connection.prepareStatement(query);
            ps.setString(1, propertyValue);
            ps.setInt(2, roid);
            affectedRows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                ps.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        return affectedRows;
    }

    public Integer deleteRegisterorder(Integer roid) {
        Integer affectedRows = 0;
        String query = "DELETE FROM registerordery WHERE roid=?";
        try (Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(query)) {
            ps.setInt(1, roid);
            affectedRows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return affectedRows;
    }

    public Integer insertRegisterorder(RegisterOrder registerOrder) {
        Integer affectedRows = 0;
        String query = "INSERT INTO registerordery(roid, memberid, departid, docid, rotime, status, optime) VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(query)) {
            ps.setInt(1, registerOrder.getRoid());
            ps.setInt(2, registerOrder.getMemberid());
            ps.setInt(3, registerOrder.getDepartid());
            ps.setInt(4, registerOrder.getDocid());
            ps.setString(5, registerOrder.getRotime());
            ps.setInt(6, registerOrder.getStatus());
            ps.setString(7, registerOrder.getOptime());
            affectedRows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return affectedRows;
    }
}